const GetPieOption = (LegendList, ListData,days) => {
  let legendList = LegendList
  let name1 = legendList.filter((item, index) => {
    if (index < (legendList.length / 2)) {
      return item.name
    }
    return null
  })
  let name2 = legendList.filter((item, index) => {
    if (index >= (legendList.length / 2)) {
      return item.name
    }
    return null
  })

  return {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: [
      {
        orient: 'horizontal',
        itemWidth: 16,
        itemHeight: 6,
        textStyle: {
          fontSize: 14,
          color: '#fff'
        },
        // formatter: function (name) {
        //   return (name.length > 5 ? (name.slice(0, 4) + '...') : name)
        // },
        formatter:function (name) {
          let data = ListData;
          let total = 0;
          let tarValue;
          for (let i = 0; i < data.length; i++) {
              total += data[i].value;
              if (data[i].name === name) {
                  tarValue = data[i].value;
              }
          }
          let p = Math.round(((tarValue / total) * 100)); // 保留整数
          // let p = ((tarValue / total) * 100).toFixed(2); // 保留2位小数
          return `${name}  ${tarValue} \n\n占比:  ${p}%`;  // 图例显示数值和百分比
        },
        itemGap: 20,
        right:10,
        top: 40,
        data: name1.map(item => {
          return item.name
        })
      },
      {
        orient: 'horizontal',
        bottom: 40,
        itemWidth: 16,
        itemHeight: 6,
        textStyle: {
          fontSize: 14,
          color: '#fff'
        },
        formatter:function (name) {
          let data = ListData;
          let total = 0;
          let tarValue;
          for (let i = 0; i < data.length; i++) {
              total += data[i].value;
              if (data[i].name === name) {
                  tarValue = data[i].value;
              }
          }
          let p = Math.round(((tarValue / total) * 100)); // 保留整数
          return `${name}  ${tarValue} \n\n占比:  ${p}%`;  // 图例显示数值和百分比
        },
        // formatter: function (name) {
        //   return (name.length > 5 ? (name.slice(0, 4) + '...') : name)
        // },
        itemGap: 20,
        right:10,
        data: name2.map(item => {
          return item.name
        })
      }
    ],
    toolbox: {
      show: true,
      feature: {
        mark: { show: true }
      }
    },
    series: [
      {
        name: '物料库龄情况',
        type: 'pie',
        radius: ['30%', "75%"],
        center: ['20%', '50%'],
        roseType: 'radius',
        itemStyle: {
          borderRadius: 5
        },
        label: {
          show: true,
          position: 'center',
			    color:'#ffffff',
          formatter: `总库龄\n${days}天`,
        },
        emphasis: {
          label: {
            show: true
          }
        },
        data: ListData
      }
    ]
  };
}

export default GetPieOption